package org.six11.ss.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.six11.ss.client.olive.OliveDrawingSurface;
import org.six11.ss.client.olive.OliveState;
import org.six11.ss.shared.GameStatusConstants;
import org.six11.util.Debug;
import org.six11.util.adt.MultiState;
import org.six11.util.gui.ColoredTextPane;
import org.six11.util.gui.InfoTextBox;
import org.six11.util.gui.PieChart;
import org.six11.util.layout.FrontEnd;
import static org.six11.util.layout.FrontEnd.*;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class ReplayPanel extends JPanel {

  private InfoTextBox clueText;
  private MultiState state;
  private PieChart pie;

  /**
   * @param multiState
   */
  public ReplayPanel(MultiState multiState) {
    this.state = multiState;
    state.addChangeListener(ClientStateNames.K_GAME_STATUS, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (GameStatusConstants.STATUS_REPLAY.equals(evt.getNewValue())) {
          // set the timer that counts down until the end of the replay phase
          long whenElapsed = (Long) state.getValue(ClientStateNames.K_ROUND_ELAPSED);
          pie.setTotalTime(whenElapsed - System.currentTimeMillis());
          pie.resetTimer();
          pie.startTimer();

          // show a message saying time is up, since it isn't totally obvious without noise.
          state.setValue(ClientStateNames.K_GLASS_MESSAGE, "Time's Up!");
        }
      }
    });
    setBackground(Color.WHITE);
    FrontEnd fe = new FrontEnd();
    clueText = new InfoTextBox();
    clueText.setFont(new Font("Dialog", Font.PLAIN, 18));
    OliveState oliveState = (OliveState) multiState.getValue(ClientStateNames.K_OLIVE_STATE);
    OliveDrawingSurface oliveSurface = new OliveDrawingSurface(oliveState);
    Dimension sz = new Dimension(400, 400);
    oliveSurface.setPreferredSize(sz);
    oliveSurface.setMaximumSize(sz);
    pie = new PieChart();
    pie.setPreferredSize(new Dimension(100, 100));

    fe.add(clueText, "clue");
    fe.add(oliveSurface, "olive");
    fe.add(pie, "pie");

    fe.addRule(ROOT, N, "clue", N);
    fe.addRule(ROOT, W, "clue", W);
    fe.addRule("pie", W, "clue", E);

    fe.addRule(ROOT, N, "pie", N);
    fe.addRule(ROOT, E, "pie", E);

    fe.addRule("pie", S, "olive", N);
    fe.addRule(ROOT, W, "olive", W);
    fe.addRule(ROOT, E, "olive", E);

    setLayout(new BorderLayout());
    add(fe, BorderLayout.CENTER);

    state.addChangeListener(ClientStateNames.K_LABELS, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        whackUI();
      }
    });
    state.addChangeListener(ClientStateNames.K_CLUE, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        whackUI();
      }
    });
  }

  @SuppressWarnings("unchecked")
  private void whackUI() {
    TextConsole text = (TextConsole) state.getValue(ClientStateNames.K_TEXT_CONSOLE);
    String clue = state.getString(ClientStateNames.K_CLUE);
    List<LabelInfo> labels = (List<LabelInfo>) state.getValue(ClientStateNames.K_LABELS);
    if (labels != null) {
      for (LabelInfo li : labels) {
        text.addOtherPlayerLabel(li.getLabelerName(), li.getText());
      }
      if (clue != null && !clue.equals("null") && clue.length() > 0) {
        text.addClue(clue);
      }
    }
    clueText.setText("I don't know what the clue was.");
    if (clue != null) {
      clueText.setText("");
      clueText.append(Color.BLACK, "The clue was: " + clue);
    }
  }

  private static void bug(String what) {
    Debug.out("ReplayPanel", what);
  }

}
